package com.bops.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bops.cache.UserCache;
import com.bops.cache.UserCache.UserCO;
import com.bops.cons.NewTypeEnum;
import com.bops.cons.TextCons;
import com.bops.dal.information.dataobject.NewsInfoDO;
import com.bops.dal.information.dataobject.NewsTypeDo;
import com.bops.valve.webuser.BopsWebUser;

public class ViewTools {
     
    /**
     * 新闻类型
     * */
	 public static List<String> getNewsTypeList() {
	        if (newsTypeList == null) {
	        	newsTypeList = new ArrayList<String>();
	        	newsTypeList.add(NewTypeEnum.getEnum(1));
	        	newsTypeList.add(NewTypeEnum.getEnum(2));
	        	newsTypeList.add(NewTypeEnum.getEnum(3));
	        }
	        return newsTypeList;

	    }


    /**
     * 检查某个人是否存在
     * 
     * @param model
     * @param engId 人员ID
     * @param txt 出错的提示语句
     * @return
     */
    public static boolean checkEngId(Map<String, Object> model, String engId, String txt) {
        if (engId == null || engId.trim().length() == 0) {
            model.put("errorStatus", "保存错误，" + txt + "不能为空，请检查!");
            return false;
        }
        UserCO userCO = UserCache.getUserCO(engId);
        if (userCO == null) {
            model.put("errorStatus", "保存错误，" + txt + "(" + engId + ")不存在，请检查!");
            return false;
        }
        return true;
    }

    /**
     * 查看当前用户是否存在权限
     * 
     * @return
     */
    public boolean hasPermission(String roleId) {
        BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
        if (bopsWebUser == null) {
            return false;
        }
        return bopsWebUser.hasPermission(roleId);
    }

    /**
     * 返回当前用户登陆对象
     * 
     * @return
     */
    public static UserCO getCurrentUser() {
        BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
        if (bopsWebUser == null) {
            return null;
        }
        UserCO userCO = UserCache.getUserCO(bopsWebUser.getMemberId());
        return userCO;
    }


    /**
     * 根据engID，返回用户对象
     * 
     * @param engId
     * @return
     */
    public static UserCO getUserCO(String engId) {
        return UserCache.getUserCO(engId);
    }

    public static boolean isCheckName(String tempName) {
        if (tempName == null) {
            return false;
        }
        if (tempName.startsWith("C") || tempName.startsWith("Z") || tempName.startsWith("W")
            || tempName.toUpperCase().startsWith("L") || tempName.toUpperCase().startsWith("D")) {
            return true;
        }
        return false;
    }

    public static String getNameNoLinkById(Integer userId) {
        if (userId == null) {
            return null;
        }
        UserCO uco = UserCache.getUserCO(userId);
        if (uco == null) {
            return "" + userId;
        }
        return uco.getEngineer();
    }

    public static String getNameNoLink(String tempName) {
        if (tempName == null) {
            return null;
        }
        if (isCheckName(tempName)) {
            String name = UserCache.getUserName(tempName.trim());
            if (name == null) {
                return tempName;
            } else {
                return name.trim();
            }
        }
        return tempName;
    }

    public static String getArray(String[] array, int index) {
        if (index == -1) {
            return null;
        }
        if (index >= array.length) {
            return null;
        }
        return array[index];
    }

    /**
     * A:转换1.00001 和0.999999之类的字符串 B:把12.0之类的字符转换为12 C:只是保留一位小数
     * 
     * @param f
     * @return
     */
    public static String floatText(float f) {
        int intF = (int) f;
        if (intF == f) {
            return "" + intF;
        }
        f = f + 0.00000001f;
        // 四舍五入取一位小数
        if (f >= 0) {
            f = f * 10 + 0.5f;
        } else {
            f = f * 10 - 0.5f;
        }
        f = ((int) f) / 10f;
        // 再一次把整数去掉
        intF = (int) f;
        if (intF == f) {
            return "" + intF;
        }
        return "" + f;
    }

    public static String floatText(Float f) {
        if (f == null) {
            return "0";
        }
        return floatText(f.floatValue());
    }

    public static String formatSimpleDate(Date date) {
        if (date == null) {
            date = new Date();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    public static String formatSimpleDateNull(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    public static String formatSimpleDateNullDetail(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static Date stringToDate(String dateStr) {
        if (dateStr == null || dateStr.trim().length() == 0) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }

    public static String formatSimpleDateAdd(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(date);
    }
    
    public static String formatSimpleDateYear(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        return simpleDateFormat.format(date);
    }


    /**
     * 返回部门
     * 
     * @return
     */
    public static List<String> getDepartmentList() {
        return getDepartmentList("false");
    }
    /**
     * 返回部门
     * @param showLigang  ture表示显示离职
     * @return
     */
    public static List<String> getDepartmentList(String showLizhi) {
        if (departmentList == null) {
            departmentList = new ArrayList<String>();
            departmentListLizhi = new ArrayList<String>();
            for (String s : TextCons.DepartmentSet) {
                departmentList.add(s);
                if (!"离职".equals(s))
                {
                    departmentListLizhi.add(s);
                }
            }
        }
        return "true".equals(showLizhi)?departmentList:departmentListLizhi;
    }

    
    /**
     * 返回部门
     * 
     * @return
     */
    public static String[] getDepartment() {
    	String [] str = TextCons.DepartmentSet;  
        List<String> list = new ArrayList<String>();  
        int a=0;
        for (int i=0; i<str.length; i++) {  
            list.add(str[i]);  
            if ("离职".equals(list.get(i)))
            {
            	a=i;
            }
        }  
        list.remove(a);
        String[] newStr =  list.toArray(new String[list.size()]); 

        return newStr;
    }
    
    


    /**
     * 返回公司
     * 
     * @return
     */
    public static String[] getCompanyList() {
        return TextCons.CompanySet;
    }
    
   





    public static String getUser() {
        BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
        UserCO usercache;
        usercache = UserCache.getUserCO(bopsWebUser.getName());
        return usercache.getEngId();
    }

    /**
     * 返回符合要求的用户列表
     * 
     * @param company
     * @param dep
     * @param engID
     * @return
     */
    public static List<UserCO> getUserList(String company, String dep, String engID) {

        company = "".equals(company) ? null : company;
        dep = "".equals(dep) ? null : dep;
        engID = "".equals(engID) ? null : engID;

        List<UserCO> list = new ArrayList<UserCO>();
        List<UserCO> allList = UserCache.getUserCOList();
        for (UserCO t : allList) {
            if (engID != null) {
                if (engID.equalsIgnoreCase(t.getEngId())) {
                    list.add(t);
                } else {
                    continue;
                }
            } else if (dep != null && company != null) {
                if (company.equalsIgnoreCase(t.getCompany()) && dep.equalsIgnoreCase(t.getDepartment())) {
                    list.add(t);
                } else {
                    continue;
                }
            } else if (company != null) {
                if (company.equalsIgnoreCase(t.getCompany())) {
                    list.add(t);
                } else {
                    continue;
                }
            } else {
                list.add(t);
            }
        }
        return list;
    }

    private static List<String> departmentList,departmentListLizhi;

    private static List<String> newsTypeList;



}
